<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="设备名称" prop="name" v-show="showSearch">
        <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备号" prop="deviceId" >
        <el-input v-model="queryParams.deviceId" placeholder="请输入设备号" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>-->
<!--      </el-form-item>-->
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="设备编号" align="center" prop="deviceId" width="140" fixed="left"/>
      <el-table-column label="设备名称" align="center" prop="name" width="180" />
      <el-table-column label="设备类型" align="center" prop="type" width="120" />
      <el-table-column label="生产厂家" align="center" prop="producer" width="120" />
      <el-table-column label="生产日期" align="center" prop="produceTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修日期" align="center" prop="keepEndTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.keepEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装地址" align="center" prop="location" width="120" />
      <el-table-column label="设备状态" align="center" prop="status" width="120" >
        <template v-slot="{row}">
          <span v-if="row.status==='运行中'" style="color: green">{{row.status}}</span>
          <span v-else style="color: red">{{row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column label="入网信息" align="center" prop="net" width="120" />
      <el-table-column label="开放端口" align="center" prop="port" width="120" />
      <el-table-column label="持续在线时长" align="center" prop="last" width="200" />
<!--      <el-table-column label="设备控制" align="center" prop="" width="120">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleControl(scope.row)"-->
<!--          >控制</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleItem(scope.row)"
          >明细</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->

<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="900px" >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
          <el-row>
            <el-form-item label="生产日期" prop="produceTime">
              <el-date-picker v-model="form.produceTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              :style="{width: '500px'}" placeholder="请选择生产日期" clearable></el-date-picker>
            </el-form-item>
            <el-form-item label="保修日期" prop="keepEndTime">
              <el-date-picker v-model="form.keepEndTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              :style="{width: '500px'}" placeholder="请选择保修日期" clearable></el-date-picker>
            </el-form-item>
            <el-form-item label="安装地址" prop="location">
              <el-input v-model="form.location" placeholder="请输入安装地址" clearable :style="{width: '500px'}"></el-input>
            </el-form-item>
            <el-form-item label="入网信息" prop="net">
              <el-input v-model="form.net" placeholder="请输入入网信息" clearable :style="{width: '500px'}">
              </el-input>
            </el-form-item>
            <el-form-item label="设备位置" prop="net">
              <baidumap ref="baidumap" :shou-map-text="true" :edit-point="form"  @getPosition="getPosition"></baidumap>
              <span>经度：{{form.longitude}}</span>
              <span style="margin-left: 20px">纬度：{{form.latitude}}</span>
            </el-form-item>
          </el-row>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, listAll, get, add, update, del } from "@/api/device/device";
import { getDicts } from "@/api/system/dict/data";
import {blurRule, changeRule} from "@/utils/formRules";
import Baidumap from "@/components/Baidumap/index.vue";
export default {
  name: "Temperature",
  components: {Baidumap},
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      rowList: [],
      menuOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: "水温监测设备",
        name: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        deviceId:blurRule('请输入设备编号'),
        name:blurRule('请输入设备名称'),
        status:changeRule('请选择设备状态')
      },
    };
  },
  created() {
    this.getList();
    this.getType();
  },
  methods: {
    handleControl(row) {
      this.$modal.msgWarning("改设备无法操作");
    },
    handleExport() {
      this.download('device/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    },
    async getType(){
      const {data} = await getDicts('corporation_status')
      this.menuOptions = data.map(item=>({label:item.dictLabel,value:item.dictValue}))
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.$refs.baidumap.clearOverlays()
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {
        type: "水温监测设备"
      };
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除设备编号为"' + row.deviceId + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //查看明细
    handleItem(row){
      this.$router.push({
        path:"/device/record",
        query:{
          deviceId:row.deviceId,
        }
      });
    },
    //获取地理坐标
    getPosition(data){
      this.$set(this.form,'longitude',data.lng)
      this.$set(this.form,'latitude',data.lat)
    },
  }
};
</script>
